package cn.com.czpb.client.module.live.adapter;


import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout.LayoutParams;

import com.bumptech.glide.Glide;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.App;
import cn.com.czpb.client.model.live.LiveItemBean;
import cn.com.czpb.client.module.news.adapter.RecyclingPagerAdapter;
import cn.com.czpb.client.utils.PageCtrl;

import java.util.ArrayList;
import java.util.List;

public class TopviewpagerLiveAdapter extends RecyclingPagerAdapter {
	
	private List<LiveItemBean> list;
	private String tid;
	private Activity context;
	
	public TopviewpagerLiveAdapter(Activity mcontext){
		list=new ArrayList<>();
		this.context=mcontext;
	}
	
	public LiveItemBean getBean(int position){
		if(list.size()==0){
			return null;
		}
		return list.get(position%list.size());
	}

	public void setData(List<LiveItemBean> mViewPagelist){
		clearData();
		addData(mViewPagelist);
	}
	
	public int getListSize(){
		return list.size();
	}
	
	public void addData(List<LiveItemBean> mViewPagelist){
		list.addAll(mViewPagelist);
		notifyDataSetChanged();
	}
	public void clearData(){
		list.clear();
	}
	public void setTid(String mtid){
		this.tid=mtid;
	}
	
	@Override
	public int getCount() {
		return list.size();
	}
	
   
    public int getPosition(int position) {
    	if(list.size()==0){
    		return 0;
    	}
        return position % list.size() ;
    }
    
	@Override
	public View getView(int position, View convertView, ViewGroup container) {
		ImageView iv=new ImageView(context);
		LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT
				, LayoutParams.MATCH_PARENT);
		iv.setLayoutParams(params);
		iv.setScaleType(ScaleType.CENTER_CROP);
		final LiveItemBean nplb=list.get(getPosition(position));
		Glide.with(App.getContext())
				.load(nplb.getPic())
				.crossFade()
				.placeholder(R.drawable.default_bg)
				.into(iv);
		iv.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){

//				NewsBean nb=new NewsBean();
//				nb.setNid(nplb.getNid());
//				nb.setTitle(nplb.getTitle());
//				nb.setUpdateTime(nplb.getUpdateTime());
//				nb.setTid(nplb.getTid());
//				nb.setComcount("-1");
//				List<String> s = new ArrayList<String>();
//				s.add(nplb.getImgurl());
//				nb.setImgs(s);
//				nb.setType("1");
//				in.putExtra("newbean", nb);
//				in.putExtra("tid", tid);
//				in.putExtra("from", "news");
//				in.setClass(context, NewsDetailActivity.class);
//				context.startActivity(in);
				PageCtrl.start2LiveDetailActivity(context,nplb);
			}
		});
		
		return iv;
	}

}
